//
//  GSArticleTitleView.m
//  GameSky
//
//  Created by lanou on 16/1/13.
//  Copyright © 2016年 NiRongfei. All rights reserved.
//

#import "GSArticleTitleView.h"

#define kMargin 10
#define kTitleFont 20

@implementation GSArticleTitleView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // 夜间模式
        self.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor grayColor]);
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.font = [UIFont systemFontOfSize:kTitleFont];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.titleLabel];
        
        self.subTitleLabel = [[UILabel alloc] init];
        self.subTitleLabel.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor grayColor]);
        self.subTitleLabel.font = [UIFont systemFontOfSize:12];
        self.subTitleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.subTitleLabel];
        
        self.lineView = [[UIView alloc] init];
        self.lineView.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:self.lineView];
    }
    return self;
}

- (void)setModel:(GSNewsArticleModel *)model {
    _model = model;
    self.titleLabel.text = model.title;
    self.subTitleLabel.text = model.subTitle;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect titleRect = [self.titleLabel.text boundingRectWithSize:CGSizeMake(kScreenWidth - kMargin * 2, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kTitleFont]} context:nil];
    self.titleLabel.frame = CGRectMake(kMargin, 0, kScreenWidth - kMargin * 2, titleRect.size.height);
    self.subTitleLabel.frame = CGRectMake(self.titleLabel.left, self.titleLabel.bottom + kMargin, self.titleLabel.width, 20);
    self.lineView.frame = CGRectMake(self.titleLabel.left, self.subTitleLabel.bottom + kMargin, self.titleLabel.width, 1);
}

+ (CGFloat)titleHeightForModel:(GSNewsArticleModel *)model {
    CGRect titleRect = [model.title boundingRectWithSize:CGSizeMake(kScreenWidth - kMargin * 2, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kTitleFont]} context:nil];
    return titleRect.size.height + kMargin * 2 + 21;
}

@end
